﻿package _1_LabelAndButton;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
 * 2011-5-31 21:01:52
 * @author Administrator
 */
public class TRadioButtonAndJCheckBox {
    JFrame frame;
    Container contentPane;
    JRadioButton colorButton[],sizeButton[];
    JCheckBox styleCheck[];
    JLabel showLabel;
    JPanel colorPane,sizePane,stylePane;
    final String colorText[]={"红色","绿色","蓝色"};
    final String sizeText[]={"20","40","60"};
    final String styleText[]={"斜体","粗体"};
    final int styleValue[]={Font.ITALIC,Font.BOLD};
    public TRadioButtonAndJCheckBox(){
        frame=new JFrame("测试JRadioButton");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane=frame.getContentPane();
        contentPane.setLayout(new GridBagLayout());
        /*
         * 颜色相关
         */
        colorPane=new JPanel();
        colorButton=new JRadioButton[colorText.length];
        colorPane.setLayout(new GridLayout(colorButton.length,1));
        colorPane.setBorder(BorderFactory.createTitledBorder("颜色选择："));
        colorPane.setPreferredSize(new Dimension(300,300));
        for(int i=0;i<colorButton.length;i++){
            colorButton[i]=new JRadioButton(colorText[i]);
            colorButton[i].addChangeListener(new ChangeListener(){
                public void stateChanged(ChangeEvent e) {
                    int rgb=0;
                    final int rgbArray[]={0xff0000,0xff00,0xff};
                    for(int i=0;i<rgbArray.length;i++){
                        if(colorButton[i].isSelected()){
                            rgb+=rgbArray[i];
                        }
                    }
                    showLabel.setForeground(new Color(rgb));
                }
            });
            colorPane.add(colorButton[i]);
        }
        add(contentPane, colorPane, 0, 0, 1, 1, 1, 1);
        /*
         * 大小相关
         */
        sizePane=new JPanel();
        sizeButton=new JRadioButton[sizeText.length];
        sizePane.setBorder(BorderFactory.createTitledBorder("字体大小选择："));
        sizePane.setLayout(new GridLayout(sizeButton.length,1));
        sizePane.setPreferredSize(new Dimension(300,300));
        ButtonGroup group=new ButtonGroup();
        for(int i=0;i<sizeButton.length;i++){
            sizeButton[i]=new JRadioButton(sizeText[i]);
            sizeButton[i].addChangeListener(new ChangeListener(){
                public void stateChanged(ChangeEvent e) {
                    JRadioButton r=(JRadioButton)e.getSource();
                    if(r.isSelected()){
                        Font font=showLabel.getFont();
                        showLabel.setFont(new Font(font.getName(),font.getStyle(),Integer.parseInt(r.getText())));
                    }
                }
            });
            sizePane.add(sizeButton[i]);
            group.add(sizeButton[i]);
        }
        add(contentPane, sizePane, 1, 0, 1, 1, 1, 1);
        showLabel=new JLabel("显示大小及颜色");
        showLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        showLabel.setPreferredSize(new Dimension(600,60));
        add(contentPane, showLabel, 0, 1, 2, 1, 1, 1);
        /*
         * 样式相关
         */ 
        stylePane=new JPanel();
        stylePane.setBorder(BorderFactory.createTitledBorder("样式设置："));
        stylePane.setLayout(new GridLayout(styleText.length,1));
        stylePane.setPreferredSize(new Dimension(300,300));
        styleCheck=new JCheckBox[styleText.length];
        for(int i=0;i<styleCheck.length;i++){
            styleCheck[i]=new JCheckBox(styleText[i]);
            styleCheck[i].addChangeListener(new ChangeListener(){
                public void stateChanged(ChangeEvent e) {
                    int value=Font.PLAIN;
                    for(int i=0;i<styleValue.length;i++){
                        if(styleCheck[i].isSelected()){
                            value+=styleValue[i];
                        }
                    }
                    Font font=showLabel.getFont();
                    showLabel.setFont(new Font(font.getName(),value,font.getSize()));
                }
            });
            stylePane.add(styleCheck[i]);
        }
        add(contentPane, stylePane, 2, 0, 1, 1, 1, 1);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public void add(Container main,Container child,int gridx,int gridy,int gridWidth,int gridHeight,double weightx,double weighty){
        GridBagConstraints c=new GridBagConstraints();
        c.gridx=gridx;
        c.gridy=gridy;
        c.gridwidth=gridWidth;
        c.gridheight=gridHeight;
        c.weightx=weightx;
        c.weighty=weighty;
        c.fill=GridBagConstraints.BOTH;
        main.add(child,c);
    }
    public static void main(String args[]){
        new TRadioButtonAndJCheckBox();
    }
}
